package com.example.demo.log;

import com.example.demo.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "用户信息管理")
@RestController
public class TestController {

    @Resource
    private UserMapper userMapper;
    @ApiOperation("修改数据")
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public Mono<User> test(@RequestBody @Validated User params) {

        userMapper.insertUser(params);

//        System.out.println(1/0);
        return Mono.just(params);
    }

    /**
     * 员工表关联部门表
     */
//    @Test
//    public void twoTableQuery() {
//        // 1、消除@DBRef引用对象中的"$id"的"$"符号
//        RemoveDollarOperation removeDollarOperation = new RemoveDollarOperation("newDepartmentFieldName", "department");
//
//        // 2、使用mongodb $lookup实现左连接部门表
//        LookupOperation lookupOperation = LookupOperation.newLookup().from("department")
//                .localField("newDepartmentFieldName.id").foreignField("_id").as("newDepartment");
//
//        // $match条件筛选
//        // MatchOperation matchOperation = new MatchOperation(Criteria.where("newDepartment.departmentName").is("信息开发系统"));
//
//        // 3、Aggregation管道操作（还可以加入$match、$project等其他管道操作，但是得注意先后顺序）
//        TypedAggregation aggregation = Aggregation.newAggregation(Employee.class, removeDollarOperation, lookupOperation);
//        // TypedAggregation aggregation = Aggregation.newAggregation(Employee.class, removeDollarOperation, lookupOperation, matchOperation);
//        AggregationResults<Document> results = mongoTemplate.aggregate(aggregation, Document.class);
//
//        System.out.println(JSONArray.toJSONString(results.getMappedResults()));
//    }


}